package com.letchat.service.impl;

import com.letchat.mapper.UserMapper;
import com.letchat.pojo.Result;
import com.letchat.pojo.User;
import com.letchat.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class UserService {
    @Autowired
    private UserMapper userMapper;

    /**
     * 登录
     * @param user
     * @return
     */
    public String login(User user) {
        User u = userMapper.getUserByUsernameAndPassword(user);
        if(u!=null){
            Map<String, Object> claim = new HashMap<>();
            claim.put("id",u.getId());
            claim.put("name",u.getName());
            claim.put("imgCode",u.getImgCode());
            return JwtUtils.generateJwt(claim);
        }
        return null;
    }

    /**
     * 根据id查询用户
     * @param id
     * @return
     */
    public User getUserById(Integer id) {
        return userMapper.selectById(id);
    }

    /**
     * 修改用户
     * @param user
     */
    public String updateUser(User user) {
        userMapper.updateById(user);
        User u = userMapper.selectById(user.getId());
        Map<String, Object> claim = new HashMap<>();
        claim.put("id",u.getId());
        claim.put("name",u.getName());
        claim.put("imgCode",u.getImgCode());
        return JwtUtils.generateJwt(claim);
    }
}
